package com.aistar.serviceVO.impl;

import com.aistar.mapper.CustomerMapper;
import com.aistar.mapper.DynamicCommentMapper;
import com.aistar.mapper.DynamicMapper;
import com.aistar.pojo.Customer;
import com.aistar.pojo.Dynamic;
import com.aistar.pojo.DynamicComment;
import com.aistar.pojo.DynamicCommentExample;
import com.aistar.pojoVO.DynamicCommentVO;
import com.aistar.serviceVO.DynamicCommentVOService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DynamicCommentVOServiceImpl implements DynamicCommentVOService {
    private final DynamicCommentMapper dynamicCommentMapper;
    private final CustomerMapper customerMapper;
    private final DynamicMapper dynamicMapper;

    @Autowired
    public DynamicCommentVOServiceImpl(
            DynamicCommentMapper dynamicCommentMapper,
            CustomerMapper customerMapper,
            DynamicMapper dynamicMapper) {
        this.dynamicCommentMapper = dynamicCommentMapper;
        this.customerMapper = customerMapper;
        this.dynamicMapper = dynamicMapper;
    }

    @Override
    public ServerResponse getByFatherId(Integer dcFatherId) {
        DynamicCommentExample dynamicCommentExample = new DynamicCommentExample();
        dynamicCommentExample.createCriteria()
                .andFatherCommentIdEqualTo(dcFatherId);
        List<DynamicCommentVO> dynamicCommentVOList = this.list2VOList(dynamicCommentExample);
        if (dynamicCommentVOList.size() > 0)
            return ServerResponse.getSuccess(dynamicCommentVOList);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByFatherDynamicId(Integer dynamicId) {
        DynamicCommentExample dynamicCommentExample = new DynamicCommentExample();
        dynamicCommentExample.createCriteria()
                .andDynamicIdEqualTo(dynamicId)
                .andFatherCommentIdIsNull();
        List<DynamicCommentVO> dynamicCommentVOList = this.list2VOList(dynamicCommentExample);
        if (dynamicCommentVOList.size() > 0)
            return ServerResponse.getSuccess(dynamicCommentVOList);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        DynamicCommentExample dynamicCommentExample = new DynamicCommentExample();
        dynamicCommentExample.createCriteria()
                .andCustomerIdEqualTo(customerId);
        dynamicCommentExample.setOrderByClause("customer_comment_time desc");
        List<DynamicCommentVO> dynamicCommentVOList = this.list2VOList(dynamicCommentExample);
        if (dynamicCommentVOList.size() > 0)
            return ServerResponse.getSuccess(dynamicCommentVOList);
        return ServerResponse.getFailed();
    }

    private List<DynamicCommentVO> list2VOList(DynamicCommentExample dynamicCommentExample) {
        List<DynamicComment> commentList = dynamicCommentMapper.selectByExample(dynamicCommentExample);
        List<DynamicCommentVO> dynamicCommentVOList = new ArrayList<>();
        for (DynamicComment dynamicComment : commentList) {
            Customer customer = customerMapper.selectByPrimaryKey(dynamicComment.getCustomerId());
            Dynamic dynamic = dynamicMapper.selectByPrimaryKey(dynamicComment.getDynamicId());
            DynamicCommentExample example = new DynamicCommentExample();
            example.createCriteria().andFatherCommentIdEqualTo(dynamicComment.getDynamicCommentId());
            Integer comment = dynamicCommentMapper.countByExample(example);
            dynamicCommentVOList.add(new DynamicCommentVO(customer, dynamicComment, comment, dynamic));
        }
        return dynamicCommentVOList;
    }
}
